
package com.oahc.bookreaderone;

import java.io.IOException;
import util.BookData;
import util.Constant;
import view.BookMenuView;
import view.BookPageFactory;
import view.BookView;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

public class BookReadActivity extends Activity {
    TextView tv_time, tv_bookname, tv_text, tv_page;

    int bookPages, currentPage = 0;

    String bookName;
    
    int bookId;

    BookPageFactory bpg;

    BookView bv;

    private static ProgressDialog pd;

    private View readViewParent;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.book_read);
        readViewParent=findViewById(R.id.book_read_parent);
        bv = (BookView)findViewById(R.id.bookview);
        bv.setParentView(this.getWindow().getDecorView());
        pd = ProgressDialog.show(this, "请稍后...", "正在读取数据,请稍后...", true, true);
        // 参数获取
        Bundle bundle = getIntent().getExtras();
        bookName = bundle.getString("bookname");
        bookId=bundle.getInt("bookid");
        bpg = new BookPageFactory(readViewParent,bv,bookId);
        try {
            bpg.loadBook(Constant.filePath + "WYDSZN" + Constant.dress, bookName);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // 自动跳转到上次阅读位置
        if (BookData.getInstance().getBookMark(bookId)[0] > 0) {
            // 上次阅读页码大于0
            final int toPage = BookData.getInstance().getBookMark(bookId)[0];
            final int processOld = BookData.getInstance().getBookMark(bookId)[1];
            Thread thread = new Thread(new Runnable() {
                public void run() {
                    // 【这里放入你耗时长的操作代码】！！！！！
                    bpg.loadBookMark(0, toPage, processOld);
                    bpg.refreshView();
                    uiHandler.sendEmptyMessage(0);
                }
            });
            thread.start();
        } else {
            pd.dismiss();
            pd = null;
        }
        bv.setBpg(bpg);
        bv.setBitmap(bpg.getmCurPageBitmap(), bpg.getmNextPageBitmap());

    }

    @Override
    public void onBackPressed() {
        if (BookMenuView.getCurrentBookMenuView().isShown()) {
            BookMenuView.getCurrentBookMenuView().dismiss();
        } else {
            bpg.saveSetting();
            super.onBackPressed();
        }
    }

    
    private static Handler uiHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    if (pd != null && pd.isShowing()) {
                        pd.dismiss();
                        pd = null;
                    }
                    break;
                default:
                    break;
            }
        }
    };
}
